#! /usr/bin/python
# -*- coding: cp1250 -*-

import os
import sys
import glob
import string

# removing dependencies from pepe library in this case
# because this is good demonstration file for python beginners
# they are to be able to run it as standalone script 

import pepe.p_txt.bigtxt as bt

def getSample(filemask, topCount=10000):
    '''Get sample of <topCount> rows.
    '''
    for inpfile in glob.glob(filemask):

        # skip directories 2010-11-10_1526 vp
        if os.path.isfile(inpfile) == False:
            pass
        else:
            lino = 0
            head = []
            tail = []
    
            # 2010-10-22_2257 vp
            ## using underscore instead dot because .# utilities 
            ## cut out everything after dot and then it is not clear 
            ## whether utility was run on original file or on output
            ## of other .# utility that processed given file
            #output = open(string.split(inpfile, '.')[0] + '.sample.#', 'wb')
            output = open(string.split(inpfile, '.')[0] + '_sample.#', 'wb')
    
            #for line in bt.lines(inpfile):
            for line in bt.lines(inpfile):
    
                lino += 1
    
                if lino < int(topCount):
                    head.append(line)
                else:
                    # exit for loop
                    break
    
            output.write("%s" % (string.join(head,'')))
            output.close()

def main(filemask):
    '''Count rows and print out TAIL and HEAD (100 rows) of text file.

    Syntax:     cht <input file mask>
    Example:    cht *.csv
    '''
    for inpfile in glob.glob(filemask):

        # skip directories 2010-11-10_1526 vp
        if os.path.isfile(inpfile) == False:
            pass
        else:
            lino = 0
            head = []
            tail = []
    
            #output = open(inpfile+'.cht.#', 'w')
            
            #fname = string.split(inpfile, '.')[:]
            
            # 2010-10-22_2257 vp
            ## using underscore instead dot because .# utilities 
            ## cut out everything after dot and then is not clear 
            ## whether utility was run on original file or on output
            ## of other .# utility that processed given file
            #output = open(string.split(inpfile, '.')[0] + '.cht.#', 'wb')
            output = open(string.split(inpfile, '.')[0] + '_cht.#', 'wb')
    
            #for line in bt.lines(inpfile):
            for line in bt.lines(inpfile):
    
                #print len(line)
                #break
                
                # not reliable 2010-09-30_2010 vp
                #lino += 1
                # becarefull to open (see class above)as 'rb', 'r' does not work
                lino += string.count(line, '\x0D\x0A')
    
                if lino < 100: 
                    head.append(line)
                    tail.append(line)
                else:
                    tail.append(line)
                    tail.pop(0)
    
            output.write("ROWS COUNT:\t%s\t%s\t%s\x0D\x0A\x0D\x0AHEAD:\x0D\x0A%s \x0D\x0A\x0D\x0ATAIL:\x0D\x0A%s" %
                         (
                          str(lino),
                          'pepe/p_utils/cht.py',
                          inpfile,
                          string.join(head,''),
                          string.join(tail,'')
                          )
                         )
            output.close()

if __name__ == '__main__':
    ''' '''
    if len(sys.argv) == 2:
        main(sys.argv[1])
    elif len(sys.argv) == 3:
        if sys.argv[2] in ('get-sample', 'getsample', 'sample'):
            getSample(sys.argv[1])
    else:
        print main.__doc__
